package net.zhangbaolin.systemadmin.common;


/**
 * REST API 错误码
 *
 * @author hubin
 * @since 2017-06-26
 */

public enum ApiErrorCode implements IErrorCode {
    /**
     * 失败
     */
    FAILED(-1, "操作失败"),
    /**
     * 成功
     */
    SUCCESS(200, "操作成功");

    private final long code;
    private final String message;

    ApiErrorCode(final long code, final String message) {
        this.code = code;
        this.message = message;
    }

    public static ApiErrorCode fromCode(long code) {
        ApiErrorCode[] ecs = ApiErrorCode.values();
        for (ApiErrorCode ec : ecs) {
            if (ec.getCode() == code) {
                return ec;
            }
        }
        return SUCCESS;
    }

    @Override
    public long getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return message;
    }

    @Override
    public String toString() {
        return String.format(" ErrorCode:{code=%s, message=%s} ", code, message);
    }
}
